package com.example.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.auth.entity.SysPermissionEntity;
import com.example.auth.service.SysPermissionService;
import com.example.mallcommon.RestResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 资源
 *
 * @author yang
 * @date 2021-08-23 23:45:04
 */
@ApiOperation("资源")
@RestController
@AllArgsConstructor
@RequestMapping("/syspermission")
public class SysPermissionController {

    @Autowired private final  SysPermissionService sysPermissionService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param sysPermission 资源
     * @return
     */
    @ApiOperation("分页查询")
    @RequestMapping(value="/page",method = RequestMethod.GET)
    public RestResult<IPage<SysPermissionEntity>> getSysPermissionPage(Page page, SysPermissionEntity sysPermission) {
        return  new RestResult<>(sysPermissionService.page(page,Wrappers.query(sysPermission)));
    }


    /**
     * id查询资源
     * @param id id
     * @return RestResult
     */
    @ApiOperation("通过id查询资源")
    @ApiImplicitParam(name = "uid",value = "资源编码", required = true,paramType = "path")
    @RequestMapping(value="/{id}",method = RequestMethod.GET)
    public RestResult<SysPermissionEntity> getById(@PathVariable("id") String id){
        return new RestResult<>(sysPermissionService.getById(id));
    }

    /**
     * 新增资源
     * @param sysPermissionEntity 资源
     * @return RestResult
     */
    @ApiOperation("新增资源")
    @ApiImplicitParam(name = "sysPermission",value = "资源实体类", required = true,paramType = "body",dataType="SysPermissionEntity")
    @RequestMapping(method = RequestMethod.POST)
    public RestResult save(@RequestBody SysPermissionEntity sysPermissionEntity){
        return new RestResult<>(sysPermissionService.save(sysPermissionEntity));
    }

    /**
     * 修改资源
     * @param sysPermissionEntity 资源
     * @return RestResult
     */
    @ApiOperation("修改资源")
    @RequestMapping(method = RequestMethod.PUT)
    @ApiImplicitParam(name = "sysPermission",value = "资源实体类", required = true,paramType = "body",dataType="SysPermissionEntity")
    public RestResult updateById(@RequestBody SysPermissionEntity sysPermissionEntity){
        return new RestResult<>(sysPermissionService.updateById(sysPermissionEntity));
    }

    /**
     * 通过id删除资源
     * @param id id
     * @return RestResult
     */
    @ApiOperation("删除资源")
    @ApiImplicitParam(name = "uid",value = "资源编码", required = true,paramType = "path")
    @RequestMapping(value="/{id}",method = RequestMethod.DELETE)
    public RestResult removeById(@PathVariable String id){
        return new RestResult<>(sysPermissionService.removeById(id));
    }

    /**
     * 查询全部
     * @param
     * @return RestResult
     */
    @ApiOperation("查询全部")
    @RequestMapping(value="/queryAll",method = RequestMethod.GET)
    public RestResult<List<SysPermissionEntity>> queryAll(){
        return new RestResult<>(sysPermissionService.list());
    }

}